package com.project.utils;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by TAO on 2015/10/24.
 */

//生成图表验证码servlet
public class CaptchaServlet extends HttpServlet {
    private static final long serialVersionUID = -124247581620199715L;

    public static final String KEY_CAPTCHA = "SE_KEY_MM_CODE";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 设置相应类型,告诉浏览器输出的内容为图片
        resp.setContentType("image/jpeg");
        // 不缓存此内容
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expire", 0);
        try {

            HttpSession session = req.getSession();

            CreateImageCode vCode = new CreateImageCode(75,25,4,10);
            session.removeAttribute(KEY_CAPTCHA);
            session.setAttribute(KEY_CAPTCHA, vCode.getCode());
            vCode.write(resp.getOutputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

